#include<stdio.h>
#include<stdlib.h>

typedef struct stu
{
    char name[128];
    char sex[50];
    int  age;
}stu;

stu data[50] = 
{
    {.name = "张三",.sex = "男",.age = 18},
    {.name = "李四",.sex = "男",.age = 18},
    {.name = "柳如烟",.sex = "女",.age = 20}
};


stu *need_size(stu *str,size_t newsize ,size_t size)
{
    
    if(str == NULL)
    {
        printf("文件打开失败\n");
        return NULL;
    } 
    if(newsize == 0)
    {
        free(str);
        str = NULL;
        return NULL;
    }
    stu *NEW = (stu *)malloc(newsize * sizeof(stu));
    if(NULL == NEW)
    {
        printf("创建失败\n");
        return NULL;
    }
    for (size_t i = 0; i < newsize; i++)
    {
        NEW[i] = str[i];
    }
    free(str); 
    str =NULL;
    return NEW;
   
}


int main(int argc, char const *argv[])
{
    int newsize,size;
    printf("请输入需要生成的空间:");
    scanf("%d",&size);
    stu *str = (stu *)malloc(size * sizeof(stu));
    for (size_t i = 0; i < size; i++)
    {
        str[i]=data[i];
        printf("姓名:%s\t性别:%s\t年龄:%d\n",str[i].name,str[i].sex,str[i].age);
    }
    printf("请输入需要生成更大的空间:");
    scanf("%d",&newsize);
    need_size(str,newsize,size);
    for (size_t i = 0; i < newsize; i++)
    {
        str[i]=data[i];
        printf("姓名:%s\t性别:%s\t年龄:%d\n",str[i].name,str[i].sex,str[i].age);
    }
    return 0;
}
